package com.spongebob.controller.xiao;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spongebob.Vo.Sessionlist;
import com.spongebob.dao.SessionMessageMapper;
import com.spongebob.pojo.CustomerService;
import com.spongebob.pojo.Session;
import com.spongebob.pojo.SessionMessage;
import com.spongebob.service.CustomerServiceService;
import com.spongebob.service.SessionMessageService;
import com.spongebob.service.SessionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会话表 前端控制器
 * </p>
 *
 * @author tutu
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/session")
public class SessionController {
    /**
     * 服务对象
     */
    @Resource
    private SessionService sessionService;
    @Resource
    SessionMessageService sessionMessageService;
    @Resource
    CustomerServiceService customerServiceService;
    @Resource
    private SessionMessageMapper sessionMessageMapper;

    /**
     * 联系管理员，如果之前没有过对话就创建一个会话
     *
     * @param httpSession
     * @param request
     * @param model
     * @return
     * @throws ParseException
     */
    @RequestMapping("/new")
    public String newSession(HttpSession httpSession, HttpServletRequest request, Model model) throws ParseException {
        List<Session> list;
        CustomerService user = (CustomerService) httpSession.getAttribute("user");
        QueryWrapper<Session> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("visitorname", user.getRealname());
        //查session
        list = sessionService.list(queryWrapper);
        //判断是否存在会话，如果不存在就创建
        if (list.size() == 0) {
            Session session = create(user.getRealname(), request.getRemoteAddr());
            list.add(session);
        }

        setcontent(httpSession, list.get(0));
        setlist2(model, list, httpSession);

        model.addAttribute("status", 21);
        return "index";
    }


    /**
     * 获取会话列表和会话内容
     */

    @RequestMapping("/getall")
    public String getSession(HttpSession session, Model model) throws ParseException {
        List<Session> l = getlist(session);

        if (l.isEmpty() || l == null) {
            model.addAttribute("status", 2);
            return "index";
        }
        setcontent(session, l.get(0));
        setlist(model, l);

        model.addAttribute("status", 2);
        return "index";
    }

    /**
     * 获取会话列表
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/get")
    public String get(HttpSession session, Model model) {
        List<Session> l = getlist(session);
        setlist(model, l);
        model.addAttribute("status", 2);
        return "index";
    }

    //通过名字查询会话列表和内容
    @RequestMapping("/search")
    public String search(HttpSession session, Model model, String searchname) {
        if (searchname == null || searchname == "") {
            return "redirect:/session/getall";
        }
        CustomerService user = (CustomerService) session.getAttribute("user");
        //查会话
        QueryWrapper<Session> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("customerserviceid", user.getCustomerserviceid());
        queryWrapper1.like("visitorname", searchname);
        Session one = sessionService.getOne(queryWrapper1);
        //查对话内容
        QueryWrapper<SessionMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sessionid", one.getSessionid());
        List<SessionMessage> list = sessionMessageService.list(queryWrapper);
        //对话
        session.setAttribute("content", list);
        session.setAttribute("visitorip", one.getVisitorip());
        session.setAttribute("sessionid", one.getSessionid());
        session.setAttribute("name", one.getVisitorname());
        session.setAttribute("active", user.getState());
        //查列表信息
        List<Sessionlist> ll = new ArrayList<>();
        SessionMessage message = sessionMessageMapper.getMessage(one.getSessionid());
        if (message == null) {
        } else {
            Sessionlist sessionlist = new Sessionlist();
            sessionlist.setContent(message.getContent());
            sessionlist.setDate(message.getTime());
            sessionlist.setName(one.getVisitorname());
            sessionlist.setSessionid(one.getSessionid());
            ll.add(sessionlist);
        }

        model.addAttribute("sessionlist", ll);

        model.addAttribute("status", 2);
        return "index";
    }


    /**
     * 获取对话内容
     *
     * @param session
     * @param one
     */
    public void setcontent(HttpSession session, Session one) throws ParseException {
        QueryWrapper<SessionMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sessionid", one.getSessionid());
        List<SessionMessage> list = sessionMessageService.list(queryWrapper);
        session.setAttribute("visitorip", one.getVisitorip());
        session.setAttribute("sessionid", one.getSessionid());
        session.setAttribute("name", one.getVisitorname());

        session.setAttribute("content", list);//对话
    }

    /**
     * 设置会话列表信息
     *
     * @param model
     * @param l
     */
    public void setlist(Model model, List<Session> l) {
        List<Sessionlist> ll = new ArrayList<>();//列表
        for (Session c : l) {
            SessionMessage message = sessionMessageMapper.getMessage(c.getSessionid());
            Sessionlist sessionlist = new Sessionlist();
            if (message != null) {
                sessionlist.setContent(message.getContent());
                sessionlist.setDate(message.getTime());

            }
            sessionlist.setName(c.getVisitorname());
            sessionlist.setSessionid(c.getSessionid());
            ll.add(sessionlist);
        }

        model.addAttribute("sessionlist", ll);
    }

    /**
     * 根据客服id获取session列表
     *
     * @param session
     * @return
     */
    public List<Session> getlist(HttpSession session) {
        CustomerService user = (CustomerService) session.getAttribute("user");
        QueryWrapper<Session> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customerserviceid", user.getCustomerserviceid());
        queryWrapper.orderByDesc("createtime");
        List<Session> list = sessionService.list(queryWrapper);
        session.setAttribute("active", user.getState());
        return list;
    }

    //获取管理员信息，得到列表信息（联系管理员）
    public void setlist2(Model model, List<Session> list, HttpSession session) {
        List<Sessionlist> ll = new ArrayList<>();//列表

        SessionMessage message = sessionMessageMapper.getMessage(list.get(0).getSessionid());

        Sessionlist sessionlist = new Sessionlist();
        if (message != null) {
            sessionlist.setContent(message.getContent());
            sessionlist.setDate(message.getTime());
        }
        CustomerService one = customerServiceService.getById(54328);
        sessionlist.setName(one.getRealname());
        sessionlist.setSessionid(list.get(0).getSessionid());
        ll.add(sessionlist);
        session.setAttribute("name", one.getRealname());
        session.setAttribute("active", one.getState());
        model.addAttribute("sessionlist", ll);
    }

    //创建会话（联系管理员）
    public Session create(String name, String ip) {
        Session session = new Session();
        session.setCustomerserviceid(54328);
        session.setCreatetime(new Date());
        session.setVisitorname(name);
        session.setVisitorip(ip);
        sessionService.save(session);
        return session;
    }
}

